package edu.cs.usask.ca.silver.plugin.rcp.views;

import java.awt.Color;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsoleStream;

/**
 * This class initializes a singleton console that is used to display message to
 * silver console.
 * 
 * use getConsole to fetch the console instance; use messageConsoleByLine(String
 * msg) to print a line of message in console; use messageConsole(String msg) to
 * print to silver console without line break.
 * 
 * @author Yudi Xue
 * 
 */
public class SilverMessageConsole extends org.eclipse.ui.console.MessageConsole {

	// private static final MessageConsole MC = new MessageConsole(
	// "Silver Console", null);

	MessageConsoleStream msgConsoleStream;
	MessageConsoleStream errConsoleStream;

	private static final org.eclipse.swt.graphics.Color BLUE = new org.eclipse.swt.graphics.Color(
			null, Color.blue.getRed(), Color.blue.getGreen(),
			Color.blue.getBlue());
	private static final org.eclipse.swt.graphics.Color RED = new org.eclipse.swt.graphics.Color(
			null, Color.red.getRed(), Color.red.getGreen(), Color.red.getBlue());

	private SilverMessageConsole(ConsoleType name, ImageDescriptor imageDescriptor) {
		super(name.toString(), imageDescriptor);
		msgConsoleStream = this.newMessageStream();
		errConsoleStream = this.newMessageStream();
		msgConsoleStream.setColor(BLUE);
		errConsoleStream.setColor(RED);
	}

	public MessageConsoleStream getMessageStream() {
		return msgConsoleStream;
	}

	public MessageConsoleStream getErrMesageStream() {
		return errConsoleStream;
	}

	public static SilverMessageConsole findConsole(ConsoleType name) {
		ConsolePlugin plugin = ConsolePlugin.getDefault();
		IConsoleManager conMan = plugin.getConsoleManager();
		IConsole[] existing = conMan.getConsoles();
		for (int i = 0; i < existing.length; i++)
			if (name.toString().equals(existing[i].getName()))
				return (SilverMessageConsole) existing[i];
		// no console found, so create a new one
		SilverMessageConsole myConsole = new SilverMessageConsole(name, null);
		conMan.addConsoles(new IConsole[] { myConsole });
		myConsole.activate();
		return myConsole;
	}

	/**
	 * send message to console followed by a line break
	 * 
	 * @param msg
	 *            String
	 */
	public void messageConsoleByLine(String msg) {
		msgConsoleStream.println(msg);
	}

	public void messageErrConsoleByLine(String msg) {
		errConsoleStream.println(msg);
	}

	public void messageExceptionConsoleByLine(String msg, Exception e) {
		errConsoleStream.println(msg + "; caused by " + e.toString());
	}

	/**
	 * send message to console followed by a line break
	 * 
	 * @param msg
	 *            String
	 */
	public void messageConsoleByLine(Boolean msg) {
		msgConsoleStream.println(String.valueOf(msg));
	}

	/**
	 * send message to console
	 * 
	 * @param msg
	 *            String
	 */
	public void messageConsole(String msg) {
		msgConsoleStream.println(msg);
	}

}
